﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="staff.aspx.cs" Inherits="OpenRAMS.Staff.WebForm1" %>
<%@ Import Namespace="OpenRAMS.Model" %>

<!DOCTYPE html />

<script runat="server">
    void btnBookRequest_Click(Object sender, EventArgs e)
    {

        Booking newBooking = new Booking();

        newBooking.bookCreator = User.Identity.Name.ToString();
        newBooking.bookUserAssigned = User.Identity.Name.ToString();
        
        //Fix apostrophes
        String titleFix = sumRequest.Text;
        titleFix = titleFix.Replace("'", @"''");
        newBooking.bookTitle = titleFix;

        String descFix = detRequest.Text;
        descFix = descFix.Replace("'", @"''");
        descFix = descFix.Replace(Environment.NewLine, "<br/>");
        newBooking.bookDesc = descFix;

        //If no date was selected, default to today
        DateTime selected;
        if (dtRequest.SelectedDate == DateTime.Parse("01/01/0001"))
        {
            selected = DateTime.Today;
        }
        else
        {
            selected = dtRequest.SelectedDate;
        }

        //test end time is larger than start time, and date set is either today or in the future.
        if (DateTime.Parse(eTimeRequest.Text) <= DateTime.Parse(sTimeRequest.Text) || selected < DateTime.Today)
        {
            lblError.Visible = true;
            lblError.Text = "Error saving record. (Did you set the dates and times correctly?)";

        }
        else
        {
            
            newBooking.bookStart = new DateTime(selected.Year, selected.Month, selected.Day, DateTime.Parse(sTimeRequest.Text).Hour, DateTime.Parse(sTimeRequest.Text).Minute, 0);
            newBooking.bookEnd = new DateTime(selected.Year, selected.Month, selected.Day, DateTime.Parse(eTimeRequest.Text).Hour, DateTime.Parse(eTimeRequest.Text).Minute, 0);

            int saveBooking = newBooking.Save();
            if (saveBooking < 0)
            {
                lblError.Visible = true;
                lblError.Text = "Error occurred during save (Did you type the wrong thing?)";
            }
            else
            {
                //If booking saved correctly, create an associated room request
                RoomRequest newRoomRequest = new RoomRequest();
                Bookings findBooking = new Bookings();    //as ID is created on the fly by SQL Server, we need to hunt for booking to get ID value
                findBooking.Populate("wontExist", "CONVERT(char,dtTimeStamp,120)='" + newBooking.createdTimeStamp.ToString("yyyy-MM-dd HH:mm:ss") + "'"); //timestamps are hard to duplicate!
                List<Booking> results = findBooking.getBookingList();
                int bID = -1;

                if (results.Count() != 1)   //if we have more than one result or no results at all, this is a very bad thing!
                {
                    lblError.Visible = true;
                    lblError.Text = "Error occurred during save (Internal error)";
                }
                else
                {
                    foreach (Booking result in results)
                    {
                        bID = result.bookID;
                    }

                    if (bID < 0)
                    {
                        lblError.Visible = true;
                        lblError.Text = "Error occurred during save (Internal error)";
                    }
                    else
                    {
                        //Now we know the ID is valid, we can create a new room request
                        newRoomRequest.BookingID = bID;

                        newRoomRequest.RoomName = locRequest.SelectedValue;
                        newRoomRequest.Requester = User.Identity.Name.ToString();

                        int saveRoomRequest = newRoomRequest.Save();
                        if (saveRoomRequest < 0)
                        {
                            lblError.Visible = true;
                            lblError.Text = "Error occurred during save (Didn't save room request)";
                        }
                        else
                        {
                            //When we're all done, send back to staff portal
                            Response.Redirect("staff.aspx");
                        }
                    }
                }
            }
        }
    }


</script>

<html>
<head id="Head1" runat="server">
    <title>OpenRAMS - Request Booking</title>
    <link rel="Stylesheet" type="text/css" href="../Include/default.css" />
    <style type="text/css">
        .style1
        {
            width: 220px;
        }
    </style>
    <%
    //Outputs logged-in username in string
    lblUserName.Text = User.Identity.Name.ToString();

    if (!IsPostBack)
    {
        dtRequest.TodaysDate = DateTime.Today;
        dtRequest.SelectedDate = dtRequest.TodaysDate;

        sTimeRequest.Text = DateTime.Now.ToString("HH:00");
        eTimeRequest.Text = DateTime.Now.AddHours(1).ToString("HH:00");
    }

    //Couldn't get the following working properly. Resorted to giving dropdown its own connection string for now
    /*
    if(!IsPostBack)
    {
        Rooms roomList = new Rooms();
        roomList.Populate();
        List<RoomRecord> rooms = roomList.getRoomList();

        ListItemCollection cmbRoomOutput = new ListItemCollection();

        foreach (RoomRecord room in rooms)
        {
            cmbRoomOutput.Add(new ListItem() { Text = room.roomName, Value = room.roomName });
        }

        locRequest.DataSource = cmbRoomOutput;
        locRequest.DataBind();
    }*/
    
%>
</head>
<body>
    <table class="tableDefault" align="center" width="90%">
    <tr><td><div class="tableTitle" align="center">Request Booking</div></td></tr>
    <tr>
    <td>
    <div class="tableBody">
    <form id="frmBookingRequest" runat="server">
        <table width="100%">
        <tr><td colspan="4" align="center"><asp:Label ID="lblError" runat="server" ForeColor="Red" Visible="false" />
        <tr>
        <td>Requester:</td>
        <td class="style1" colspan="3"><asp:Label ID="lblUserName" runat="server" Font-Bold="true" /></td>
        </tr>
        <tr>
        <td>Summary:</td>
        <td colspan="3"><asp:TextBox ID="sumRequest" runat="server" width="100%" Text="Type booking summary here." MaxLength="49"/></td>
        </tr>
        <tr>
        <td>Description:</td>
        <td colspan="3"><asp:TextBox ID="detRequest" runat="server" TextMode="MultiLine" width="100%" Height="100px" Text="Type booking description here." MaxLength="149" /></td>
        </tr>
        <tr>
       <td>Select Date:</td>
        <td align="center">
            <asp:Calendar ID="dtRequest" runat="server" BackColor="White" 
                BorderColor="White" Font-Names="Verdana" Font-Size="9pt" 
                ForeColor="Black" Height="190px" Width="350px" BorderWidth="1px" 
                NextPrevFormat="FullMonth">
            <DayHeaderStyle Font-Bold="True" Font-Size="8pt" />
            <NextPrevStyle VerticalAlign="Bottom" Font-Bold="True" Font-Size="8pt" 
                    ForeColor="#333333" />
            <OtherMonthDayStyle ForeColor="#999999" />
            <SelectedDayStyle BackColor="#333399" ForeColor="White" />
            <TitleStyle BackColor="White" BorderColor="Black" 
                Font-Bold="True" BorderWidth="4px" Font-Size="12pt" ForeColor="#333399" />
            <TodayDayStyle BackColor="#CCCCCC" />
            </asp:Calendar>
            </td>
            <td>
            <table width="100%" height="100%">
            <tr>
        <td>Start Time:</td></tr>
        <tr>
        <td width="auto"><asp:TextBox ID="sTimeRequest" runat="server" Text="09:00" TextMode="Time" /></td></tr><tr>
        <td>End Time:</td></tr>
        <tr>
        <td width="auto">
        <asp:TextBox ID="eTimeRequest" runat="server" Text="10:00" TextMode="Time" /></td>
        </tr>
        </table>
        </td>
        </tr>
        
        <tr>
        <td>Location</td>
        <td colspan="3">
            <asp:DropDownList ID="locRequest" runat="server" width="100%" 
                DataSourceID="SqlDataSource1" DataTextField="txtRoom" 
                DataValueField="txtRoom" />
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:openramsConnectionString2 %>" 
                SelectCommand="SELECT [txtRoom] FROM [tblRooms]"></asp:SqlDataSource>
            </td>
        </tr>
        <tr>
        <td colspan="3">
        <table width="100%">
        <td align="center"><asp:Button ID="btnBookRequest" Text="Submit" 
                runat="server" Width="100%" onclick="btnBookRequest_Click" /></td>
        <td align="center"><asp:Button ID="btnCancel" Text="Cancel" runat="server" width="100%" 
                onclick="btnCancel_Click"/></td>
        </tr>
        </table>
        </td>
        </tr>
        </table>
        </form>
    </div>
    </td>
    </tr>
    <tr>
    <td colspan="2"><div class="footer">
    <% 
        Response.WriteFile(Server.MapPath("~/Include/footer.txt"));
    %>
    </div></td>
    </tr>
    </table>
</body>
</html>
